<!DOCTYPE html>
<html lang="zh">
<head>
    #@header()
    #set(isAdd = operate == null ? true : false, isEdit = !isAdd)
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-role-add">
        <input hidden name="id" id="userId" value="#(operate.id??)">
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">操作名称：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="name" id="name" value="#(operate.name??)" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">操作编码：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="code" id="code" value="#(operate.code??)" required>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">描述：</label>
            <div class="col-sm-8">
                <textarea type="text" name="desc" class="form-control" placeholder="请输入文本">#(operate.desc??)</textarea>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">排序：</label>
            <div class="col-sm-8">
                <input class="form-control" type="number" name="sort" id="sort" value="#(operate.sort??0)" required>
            </div>
        </div>

    </form>
</div>
#@footer()
<script type="text/javascript">
    $("#form-role-add").validate({
        rules: {
            name: {
                required: true,
                maxlength: 20,
                remote: {
                    url: "#(prj)/admin/operate/nameUnique",
                    type: "get",
                    dataType: "json",
                    data: {
                        "id": function() {
                            return $("#userId").val();
                        },
                        "name": function() {
                            return $("input[name='name']").val();
                        }
                    },
                    dataFilter: function (data, type) {
                        var res = JSON.parse(data);
                        if(res.state == 'ok'){
                            return true
                        }else{
                            return false
                        }
                    }
                }
            },
            code: {
                required: true,
                maxlength: 20,
                remote: {
                    url: "#(prj)/admin/operate/codeUnique",
                    type: "get",
                    dataType: "json",
                    data: {
                        "id": function() {
                            return $("#userId").val();
                        },
                        "code": function() {
                            return $("input[name='code']").val();
                        }
                    },
                    dataFilter: function (data, type) {
                        var res = JSON.parse(data);
                        if(res.state == 'ok'){
                            return true
                        }else{
                            return false
                        }
                    }
                }
            },
            onkeyup: true,
        },
        messages: {
            "name": {
                remote: "名称重复"
            },
            "code": {
                remote: "编码重复"
            },
        },
        focusCleanup: true
    });
    var uurl = "";
    #if(isAdd)
        uurl = "#(prj)/admin/operate/save";
    #else
    uurl = "#(prj)/admin/operate/update";
    #end

    function submitHandler() {
        if ($.validate.form()) {
            #(isAdd ? 'add()' : 'edit()')
        }
    }

    function #(isAdd ? 'add()' : 'edit()'){

        var formData = new FormData();
        formData.append("operate.name",$("input[name='name']").val())
        formData.append("operate.code",$("input[name='code']").val())
        formData.append("operate.desc",$("textarea[name='desc']").val())
        formData.append("operate.sort",$("input[name='sort']").val())
        formData.append("operate.id",$("input[name='id']").val())

        $.ajax({
            url:uurl,
            type: "post",
            data: formData,
            contentType: false,
            processData: false,
            async: false,
            error: function (request) {
                $.modal.alertError("系统错误");
            },
            success: function (data) {
                $.operate.successCallbackMsg(data.msg);
            }
        });
    }

</script>
</body>
</html>
